import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [{
  path: '/login',
  component: () => import('@/views/login/index'),
  hidden: true
},
{
  path: '*',
  component: () => import('@/views/404'),
  hidden: true
},

{
  path: '/',
  component: Layout,
  redirect: '/dashboard',
  name: '控制面板',
  children: [{
    path: 'dashboard',
    name: 'Dashboard',
    component: () => import('@/views/dashboard/index'),
    meta: {
      title: '控制面板',
      icon: 'dashboard',
      auth: true,
      name: 'dashboard/index'
    }
  }]
}

]

export const asyncRoutes = [
  {
    path: '/agent',
    component: Layout,
    redirect: '/agent/index',
    meta: {
      title: '代理',
      icon: 'user',
      auth: true,
      name: 'agent'
    },
    alwaysShow: true,
    children: [{
      path: 'index',
      name: '代理管理',
      component: () => import('@/views/agent/index'),
      meta: {
        title: '代理管理',
        icon: 'el-icon-tickets',
        auth: true,
        name: 'agent/index'
      }
    }]
  },
  {
    path: '/device',
    component: Layout,
    redirect: '/device/index',
    meta: {
      title: '设备',
      icon: 'el-icon-s-platform',
      auth: true,
      name: 'device'
    },
    alwaysShow: true,
    children: [{
      path: 'index',
      name: '设备管理',
      component: () => import('@/views/device/index'),
      meta: {
        title: '设备管理',
        icon: 'el-icon-cloudy',
        auth: true,
        name: 'device/index'
      }
    },
    {
      path: 'group',
      name: '设备分组',
      component: () => import('@/views/device/group'),
      meta: {
        title: '设备分组',
        icon: 'el-icon-folder',
        auth: true,
        name: 'device/group'
      }
    }]
  },
  {
    path: '/tiktok-store',
    component: Layout,
    redirect: '/tiktok-store/index',
    meta: {
      title: '小店',
      icon: 'el-icon-s-shop',
      auth: true,
      name: 'tiktok-store'
    },
    alwaysShow: true,
    children: [{
      path: 'index',
      name: '使用手册',
      component: () => import('@/views/tiktok-store/index'),
      meta: {
        title: '使用手册',
        icon: 'el-icon-reading',
        auth: true,
        name: 'tiktok-store/index'
      }
    },
    {
      path: 'brush',
      name: '下单',
      component: () => import('@/views/tiktok-store/brush'),
      meta: {
        title: '下单',
        icon: 'el-icon-guide',
        auth: true,
        name: 'tiktok-store/brush'
      }
    },
    {
      path: 'task',
      name: '任务列表',
      component: () => import('@/views/tiktok-store/task'),
      meta: {
        title: '任务列表',
        icon: 'el-icon-guide',
        auth: true,
        name: 'tiktok-store/task'
      }
    },
    {
      path: 'order',
      name: '订单管理',
      component: () => import('@/views/tiktok-store/order'),
      meta: {
        title: '订单管理',
        icon: 'el-icon-guide',
        auth: true,
        name: 'tiktok-store/order'
      }
    }
    ]
  },
  {
    path: '/tiktok-task',
    component: Layout,
    redirect: '/tiktok-task/index',
    name: 'tiktok-task',
    meta: {
      title: '任务',
      icon: 'el-icon-s-platform',
      auth: true,
      name: 'tiktok-task'
    },
    alwaysShow: true,
    children: [{
      path: 'task',
      name: '任务列表',
      component: () => import('@/views/tiktok-task/task'),
      meta: {
        title: '任务列表',
        icon: 'el-icon-position',
        auth: true,
        name: 'tiktok-task/task'
      }
    },
    {
      path: 'provide',
      name: '养号',
      component: () => import('@/views/tiktok-task/provide'),
      meta: {
        title: '养号',
        icon: 'el-icon-s-platform',
        auth: true,
        name: 'tiktok-task/provide'
      }
    }

    ]
  }

]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
